package org.jef.oss.oss.client;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.utils.StringUtils;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import com.boke.oss.BokeOssClient;
import com.boke.oss.exception.ResultException;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Random;

/**
 * @version 1.0
 * @description: 重写BokeOssClient
 * @author: ChengZhi 0336
 * @create: 2024-08-30 14:25
 **/
@Slf4j
public class CustomBokeOssClient extends BokeOssClient {

    public CustomBokeOssClient(String accessKeyId, String accessKeySecret, String bucketName, String imgKey) {
        super(accessKeyId, accessKeySecret, bucketName, imgKey);
    }

    public CustomBokeOssClient(String accessKeyId, String accessKeySecret, String bucketName, String imgKey, String endpoint) {
        super(accessKeyId, accessKeySecret, bucketName, imgKey, endpoint);
    }

    @Override
    public String uploadFileOSS(InputStream isstream, String fileName, String fileSuffix) {
        OSS ossClient = getOssClient();
        String bucketName = getBucketName();
        String imgKey = getImgKey();
        try {
            if (fileSuffix.charAt(0) != '.') {
                fileSuffix = "." + fileSuffix;
            }

            if (StringUtils.isNullOrEmpty(fileName)) {
                Random random = new Random();
                fileName = (long)random.nextInt(10000) + System.currentTimeMillis() + fileSuffix;
            } else {
                fileName = fileName + fileSuffix;
            }

            ObjectMetadata objectMetadata = this.createObjectMetadata(fileName, fileSuffix, isstream.available());
            ossClient.putObject(bucketName, imgKey + fileName, isstream, objectMetadata);
        } catch (Exception var12) {
            Exception e = var12;
            throw new ResultException(e.getMessage());
        } finally {
            try {
                if (isstream != null) {
                    isstream.close();
                }
            } catch (IOException var11) {
                IOException e = var11;
                e.printStackTrace();
            }

        }

        return fileName;
    }

    private ObjectMetadata createObjectMetadata(String fileName, String fileSuffix, int available) {
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength((long)available);
        objectMetadata.setCacheControl("no-cache");
        objectMetadata.setHeader("Pragma", "no-cache");
        objectMetadata.setContentType(this.getContentType(fileSuffix));
        objectMetadata.setContentDisposition("inline;filename=" + fileName);
        return objectMetadata;
    }

    /**
     * 像Oss中设置对象
     * @param obj
     * @param fileName
     * @return
     */
    public String putObject(Serializable obj, String fileName) {
        byte[] serialize = ObjectUtil.serialize(obj);
        ByteArrayInputStream inputStream = new ByteArrayInputStream(serialize);
        String filePrix = fileName.substring(0, fileName.lastIndexOf("."));
        String fileSuffix = fileName.substring(fileName.lastIndexOf("."));
        String s = this.uploadFileOSS(inputStream, filePrix, fileSuffix);
        return s;
    }

    /**
     * 下载文件
     * @param fileName
     * @return
     */
    public OSSObject getObject(String fileName) {

        OSS ossClient = getOssClient();
        String imgKey = getImgKey();
        // 获取 OSS 对象
        OSSObject ossObject = ossClient.getObject(getBucketName(), imgKey + fileName);
        return ossObject;
    }

    /**
     * 删除文件
     * @param fileName
     */
    public void deleteObject(String fileName) {
        OSS ossClient = getOssClient();
        String imgKey = getImgKey();
        ossClient.deleteObject(getBucketName(), imgKey + fileName);
    }
    /**
     * 从oss中获取对象
     * @param objectName
     * @param clazz
     * @return
     * @param <T>
     * @throws IOException
     */
    public <T> T getObject(String objectName, Class<T> clazz) {
        OSS ossClient = getOssClient();
        String imgKey = getImgKey();
        // 获取 OSS 对象
        OSSObject ossObject = ossClient.getObject(getBucketName(), imgKey + objectName);

        // 读取对象内容为字节数组
        try (InputStream inputStream = ossObject.getObjectContent()) {
            byte[] bytes = IoUtil.readBytes(inputStream);
            // 使用 Hutool 反序列化
            return ObjectUtil.deserialize(bytes, clazz);
        } catch (Exception e) {
            log.error("从oss中获取对象并反序列化为对象时失败：", e);
        }

        return null;

    }

}
